import SoftLine from "../graphic/soft-line";

/**
 * 纹理厚度线
 *
 * @param vertices {array} 组成线的点序列
 * @param values {object} 可选参数对象
 *              {
 *                  map:null,纹理贴图
 *                  +父类的所有参数
 *              }
 *
 * @property map {texture} 纹理贴图
 *         +及父类的属性
 */
export default class TextureLine extends SoftLine{
    constructor(vertices,values={}){
        super(vertices,values);
    }

    get map(){
        return this._props.map.value;
    }
    set map(tex){
        this._props.map.value=tex;
    }

    get fragmentShader(){
        return [
            'uniform sampler2D map;' ,
            'uniform float dashSize;' ,
            'uniform float gapSize;',
            'uniform float opacity;' ,
            'uniform bool dashFill;',
            'uniform vec3 color;',
            'varying float vDistance;',
            'varying float vDashMark;',
            'varying vec2 vuv;',
            'void main(){',
            '   vec4 tex=texture2D(map,vuv+vec2(0.,0.5));' ,
            '   tex.r*=color.r;',
            '   tex.g*=color.g;',
            '   tex.b*=color.b;',
            '   if(dashFill||vDashMark==0.){' ,
            '      gl_FragColor=vec4(tex.rgb,opacity*tex.a);',
            '   }else{',
            '      float dev=mod(vDistance,dashSize+gapSize);',
            '      if(dev>dashSize){',
            '         discard;',
            '      }',
            '      gl_FragColor=vec4(tex.rgb,opacity*tex.a);',
            '   }',
            '}'
        ].join('\n');
    }

    _initProps(){
        super._initProps();
        this._props.add({
            map:this._values.map
        });
    }
    _initPropCallback() {
        super._initPropCallback();
        this._props.onKeyChange('map',(_new,_old)=>{
            this.material.uniforms.map.value=_new;
        });
    }
    _initMaterial(){
        super._initMaterial();
        this.material.uniforms.map={value:this._props.map.value};
    }
}